package com.chouxiaozi.wechatpublic.commons.util;


import java.security.MessageDigest;

public class MD5 {

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	
	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String encode(String origin) {
		String resultString = null;

		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString
					.getBytes()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return resultString;
	}
	
	public static String encode(String origin,String charset) {
		String resultString = null;

		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString
					.getBytes(charset)));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

	public static void main(String[] args) {
		try {
			System.out.println(MD5.encode("{\"areaId\":\"30\",\"areaName\":\"男爵领域\",\"assists\":\"8\",\"barracks\":\"0\",\"championId\":\"11\",\"die\":\"5\",\"endTime\":0,\"evenKill\":\"0\",\"fiveKill\":0,\"gameMode\":\"7\",\"gamerId\":\"194832139\",\"hitWild\":\"17\",\"level\":\"1\",\"mac\":\"98-40-BB-19-B1-E4\",\"machine\":\"SB001\",\"multiKill\":\"1\",\"pylon\":\"0\",\"queue\":\"255\",\"role\":\"gui8939\",\"startTime\":0,\"suerGod\":0,\"tier\":\"255\",\"uuid\":\"276937240_194832139\",\"victory\":\"1\"}?merchantId=3108644709"));
		} catch (Exception e) {

		}
	}
}
